<?php
namespace AdminBundle\Repository;


class TaskJobsRepository extends BaseRepository
{
    public function pageQuery($_params, $page)
    {
        $sql = <<<EOF
SELECT 
    IFNULL(j.id, 0) AS id,
    IFNULL(j.name, p.name) AS name,
    j.note,
    IFNULL(j.is_enabled, 0) AS isEnabled,
    j.creator,
    j.ip,
    j.ctime,
    j.start_step_id AS startStepId,
    IFNULL(j.active_start_date, p.active_start_date) AS activeStartDate,
    IFNULL(j.active_start_time, p.active_start_time) AS activeStartTime,
    IFNULL(j.active_end_date, p.active_end_date) AS activeEndDate,
    IFNULL(j.active_end_time, p.active_end_time) AS activeEndTime,
    IFNULL(j.freq_type, p.freq_type) AS freqType,
    IFNULL(j.freq_expression, p.freq_expression) AS freqExpression,
    IFNULL(j.package_id, p.id) AS packageId,
    IFNULL(j.group_id, 0) AS groupId
FROM
    task_jobs AS j
        RIGHT JOIN
    task_package AS p ON j.package_id = p.id
EOF;
        $params = array();
        if (!empty($_params)) {
            if (isset($_params['device_group'])) {
                $sql .= ' and group_id = :group_id ';
                $params['group_id'] = $_params['device_group'];
            }

            /*
                        if (isset($_params['starttime'])) {
                            $sql .= ' and j.ctime > :starttime ';
                            $params['starttime'] = $_params['starttime'];
                        }
                        if (isset($_params['endtime'])) {
                            $sql .= ' and j.ctime < :endtime ';
                            $params['endtime'] = $_params['endtime'];
                        }
            */
            if (isset($_params['keyword']) && !empty($_params['keyword'])) {
                $sql .= ' and j.name like :keyword ';
                $params['keyword'] = '%' . $_params['keyword'] . '%';
            }
        }
        $sql .= ' ORDER BY packageId';

        return $this->pageWithRawSql($sql, $params, $page);
    }

    /**
     * 根据作业id，获取其下可用的模版
     * 用途:后台ajax接口
     * @param $_params
     * @param int $page
     * @return array
     */
    public function templateByJob($_params, $page = 1)
    {
        $sql = <<<EOF
SELECT 
    t.id,
    IFNULL(s.is_enabled, 0) AS isEnabled,
    IFNULL(s.name, t.name) AS name,
    IFNULL(s.is_rand, 0) AS isRand,
    IFNULL(s.recurrence_factor, 1) AS recurrenceFactor
FROM
    cloud_control.task_job_steps AS s
        RIGHT JOIN
    cloud_control.task_template AS t ON s.template_id = t.id AND s.job_id = :job_id
ORDER BY isEnabled DESC , id;
EOF;
        $params = ['job_id' => $_params['job_id']];

        return $this->pageWithRawSql($sql, $params, $page);
    }

    /**
     * 根据作业id,获取已经分配的模版详细信息
     * 用途: 后台定时任务分发任务
     * @param $_params
     * @param int $page
     * @return bool
     * @throws \Exception
     */
    public function stepByJob($_params)
    {
        $sql = <<<EOF
SELECT 
    step.name,
    step.creator,
    step.step_id as stepId,
    step.template_id as templateId,
    step.is_rand as isRand,
    step.recurrence_factor as recurrenceFactor,
    t.uri,
    t.method,
    t.action,
    t.param
FROM
    cloud_control.task_job_steps AS step
        INNER JOIN
    task_template AS t ON step.template_id = t.id
WHERE
    step.job_id = :job_id AND step.is_enabled = 1
ORDER BY step_id
EOF;
        $params = ['job_id' => $_params['job_id']];

        return $this->rawQuery($sql, $params);
    }
}
